/**
 * 
 */
package com.feizhi.common.repo.helper.impl;

import com.feizhi.common.repo.helper.ColumnNameStrategy;

/**
 * Convert a name in camelCase to an underscored name in lower case. Any upper
 * case letters are converted to lower case with a preceding underscore.
 * <p>
 * e.g.: <br/>
 * <code>createTime</code> to <code>create_time</code>
 * </p>
 * 
 * @see BeanPropertyRowMap
 * @author shuaqiu 2013年11月16日
 */
public class UnderscodeColumnNameStrategy implements ColumnNameStrategy {

	@Override
	public String getColumnName(String prop) {
		StringBuilder column = new StringBuilder();
		if (prop != null && prop.length() > 0) {
			column.append(prop.substring(0, 1).toLowerCase());
			for (int i = 1; i < prop.length(); i++) {
				String s = prop.substring(i, i + 1);
				if (s.equals(s.toUpperCase()) && s.matches("[A-Z]")) {
					column.append("_");
					column.append(s.toLowerCase());
				} else {
					column.append(s);
				}
			}
		}
		return column.toString();
	}

}
